package com.ye.service.impl;

import com.ye.domain.Music;
import com.ye.feign.MusicFeign;
import com.ye.service.MusicService;
import com.ye.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

@Service
public class MusicServiceImpl implements MusicService {

    @Autowired
    private MusicFeign musicFeign;

    @Override
    public String uploadMusic(String fileName, File file, Integer isMusic) {
        InputStream fileInputStream = null;
        try {
//            获取文件输入流
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
//        调用封装好的的oss上传工具，返回得到url地址
        return OssUtil.startUpload(fileName, fileInputStream, isMusic);

    }

    /**
     * 全局详细搜索，当数据库中不存在所查询的数据时触发
     * @param songName
     * @return musicListByName
     */

    @Override
    public List<Music> getMusicListByName(String songName) {

        List<Music> musicListByName = musicFeign.getMusicListByName(songName);
        try {
            if (musicListByName ==null || musicListByName.size() ==0){
//                remoteMusicService.getMusicOutlineBySongName(songName);
                return musicFeign.fuzzySearch(songName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return musicListByName;
    }

    /**
     * 模糊搜索
     * @param songName
     * @return musicList
     */

    @Override
    public List<Music> fuzzySearch(String songName) {
        List<Music> musicList = null;
        try {
            musicList = musicFeign.fuzzySearch(songName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return musicList;
    }

    @Override
    public Integer countMusicNum() {
        return musicFeign.countMusicNum();
    }

    @Override
    public List<Music> rand12() {
        return musicFeign.rand12();
    }

    @Override
    public void saveMusicFromOss(Music music) {

        musicFeign.saveMusicData(music);

    }
}
